import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

// 批量导入Vue
const routeKeys = G.routeKeys = {
  __proto__: null,
  '/login': true,
  '/switchManagement': true,
  '/index': true,
};
const routes = [
  {
    path: '/',
    redirect: '/login', //重定向到login登录页
  }
];
G.import(require.context('@/views', true, /\.vue$/ /*,'lazy'*/), (obj, path) => {
  //首先获取地址
  var url = url || window.location.href;
//获取传值
  var arr = url.split("?");
//判断是否有传值
  if (arr.length !== 1) {
    localStorage.removeItem("otherModuleToPz");
    //获取get传值的个数
    var value_arr = arr[1].split("&");
    //循环生成返回的对象
    var obj = {};
    for (var i = 0; i < value_arr.length; i++) {
      var key_val = value_arr[i].split("=");
      obj[key_val[0]] = key_val[1];
    }
    if (obj !== null) {
      localStorage.setItem("userId", decodeURI(obj.userId));
      localStorage.setItem("roleId", decodeURI(obj.roleId));
      localStorage.setItem("loginName", decodeURI(obj.loginName));
      localStorage.setItem("loginAccount", decodeURI(obj.loginAccount));
      localStorage.setItem("MyPictureID", decodeURI(obj.MyPictureID));
      console.log("_______________")
      console.log(localStorage.getItem("userId"))
      console.log(localStorage.getItem("roleId"))
      console.log(localStorage.getItem("loginName"))
      console.log(localStorage.getItem("loginAccount"))
      console.log(localStorage.getItem("MyPictureID"))
      // this.$root.isLogined = true;

      switch (localStorage.getItem("roleId")) {
        case "1":
          console.log("________1_______")
          window.location.href = `http://yangzidaxingdiaozhuang.com/yz/#/organization/userManagement`
          break;
        case "2":
          console.log("________2_______")
          window.location.href = `http://yangzidaxingdiaozhuang.com/yz/#/project/projectManagement2`
          break;
        case "3":
          console.log("________3_______")
          window.location.href = `http://yangzidaxingdiaozhuang.com/yz/#/project/projectManagement3`
          break;
      }
    }
  }
  routeKeys[path = path.slice(1, -4)] = true;
  routes.push({path: path, name: obj.name || path.split('/').item(-1), component: obj});
});
// console.error(routes);
const routeIfNot = G.routeIfNot = function (hash) {
  // if ( newRouter === undefined ) return;
  // console.error( routeKeys, hash.replace('#',''), routeKeys[ hash.replace('#','') ] );
  if (!G.local('userId') || routeKeys[hash.replace('#', '')] === undefined) {
    // console.warn(newRouter);
    G.app && G.app.exit(true);
    return false;
  }
  ;
};

var newRouter;
export default newRouter = new Router({
  routes: routes
});

newRouter.beforeEach((to, from, next) => {
  // console.warn(to.path);
  routeIfNot(to.path) !== false && next();
});
